<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="账套名称" prop="accountName">
        <el-input
          v-model="queryParams.accountName"
          placeholder="请输入账套名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="单位名称" prop="companyName">
        <el-input
          v-model="queryParams.companyName"
          placeholder="请输入单位名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="税号" prop="taxNumber">
        <el-input
          v-model="queryParams.taxNumber"
          placeholder="请输入税号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="套账启用时间" prop="startDate">
        <el-date-picker
          clearable
          v-model="queryParams.startDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择套账启用时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="联系人" prop="contactPerson">
        <el-input
          v-model="queryParams.contactPerson"
          placeholder="请输入联系人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系电话" prop="contactPhone">
        <el-input
          v-model="queryParams.contactPhone"
          placeholder="请输入联系电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="增值税种类" prop="vatType">
        <el-select
          v-model="queryParams.vatType"
          placeholder="请选择增值税种类"
          clearable
        >
          <el-option
            v-for="dict in dict.type.vat_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="行业ID" prop="industryId">
        <el-input
          v-model="queryParams.industryId"
          placeholder="请输入行业ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['finance:AccountSet:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['finance:AccountSet:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['finance:AccountSet:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['finance:AccountSet:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="AccountSetList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="账套ID" align="center" prop="accountId" />
      <el-table-column label="账套名称" align="center" prop="accountName" />
      <el-table-column label="单位名称" align="center" prop="companyName" />
      <el-table-column
        label="会计准则"
        align="center"
        prop="accountingStandards"
      >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.accounting_standards"
            :value="scope.row.accountingStandards"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="统一社会信用代码"
        align="center"
        prop="creditCode"
      />
      <el-table-column label="税号" align="center" prop="taxNumber" />
      <el-table-column
        label="套账启用时间"
        align="center"
        prop="startDate"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="经营地址" align="center" prop="businessAddress" />
      <el-table-column label="联系人" align="center" prop="contactPerson" />
      <el-table-column label="联系电话" align="center" prop="contactPhone" />
      <el-table-column label="增值税种类" align="center" prop="vatType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.vat_type" :value="scope.row.vatType" />
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="当前账套标识" align="center" prop="isCurrent" />
      <el-table-column label="行业ID" align="center" prop="industryId" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['finance:AccountSet:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['finance:AccountSet:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改账套管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="账套名称" prop="accountName">
          <el-input v-model="form.accountName" placeholder="请输入账套名称" />
        </el-form-item>
        <el-form-item label="单位名称" prop="companyName">
          <el-input v-model="form.companyName" placeholder="请输入单位名称" />
        </el-form-item>
        <el-form-item label="会计准则" prop="accountingStandards">
          <el-select
            v-model="form.accountingStandards"
            placeholder="请选择会计准则"
          >
            <el-option
              v-for="dict in dict.type.accounting_standards"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="统一社会信用代码" prop="creditCode">
          <el-input
            v-model="form.creditCode"
            placeholder="请输入统一社会信用代码"
          />
        </el-form-item>
        <el-form-item label="税号" prop="taxNumber">
          <el-input v-model="form.taxNumber" placeholder="请输入税号" />
        </el-form-item>
        <el-form-item label="套账启用时间" prop="startDate">
          <el-date-picker
            clearable
            v-model="form.startDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择套账启用时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="经营地址" prop="businessAddress">
          <el-input
            v-model="form.businessAddress"
            placeholder="请输入经营地址"
          />
        </el-form-item>
        <el-form-item label="联系人" prop="contactPerson">
          <el-input v-model="form.contactPerson" placeholder="请输入联系人" />
        </el-form-item>
        <el-form-item label="联系电话" prop="contactPhone">
          <el-input v-model="form.contactPhone" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="增值税种类" prop="vatType">
          <el-radio-group v-model="form.vatType">
            <el-radio
              v-for="dict in dict.type.vat_type"
              :key="dict.value"
              :label="parseInt(dict.value)"
              >{{ dict.label }}</el-radio
            >
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            v-model="form.remark"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item label="当前账套标识" prop="isCurrent">
          <el-input v-model="form.isCurrent" placeholder="请输入当前账套标识" />
        </el-form-item>
        <el-form-item label="行业" prop="industryId">
          <el-select v-model="form.industryId" placeholder="请选择行业">
            <el-option
              v-for="dict in industry"
              :key="dict.industryId"
              :label="dict.industryName"
              :value="dict.industryId"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listAccountSet,
  getAccountSet,
  delAccountSet,
  addAccountSet,
  updateAccountSet,
  getindustry,
} from "@/api/finance/AccountSet";

export default {
  name: "AccountSet",
  dicts: ["accounting_standards", "vat_type"],
  data() {
    return {
      industry: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 账套管理表格数据
      AccountSetList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        accountName: null,
        companyName: null,
        taxNumber: null,
        startDate: null,
        contactPerson: null,
        contactPhone: null,
        vatType: null,
        industryId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        accountName: [
          { required: true, message: "账套名称不能为空", trigger: "blur" },
        ],
        companyName: [
          { required: true, message: "单位名称不能为空", trigger: "blur" },
        ],
        startDate: [
          { required: true, message: "套账启用时间不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
    this.getindustrys();
  },
  methods: {
    /** 查询账套管理列表 */
    getList() {
      this.loading = true;
      listAccountSet(this.queryParams).then((response) => {
        this.AccountSetList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    /**行业信息 */
    getindustrys() {
      getindustry().then((response) => {
        console.log(response.rows);
        this.industry = response.rows;
      });
    },

    // 表单重置
    reset() {
      this.form = {
        accountId: null,
        accountName: null,
        companyName: null,
        accountingStandards: null,
        creditCode: null,
        taxNumber: null,
        startDate: null,
        businessAddress: null,
        contactPerson: null,
        contactPhone: null,
        vatType: null,
        remark: null,
        isCurrent: null,
        industryId: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        isDeleted: null,
        version: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.accountId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加账套管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const accountId = row.accountId || this.ids;
      getAccountSet(accountId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改账套管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.accountId != null) {
            updateAccountSet(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAccountSet(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const accountIds = row.accountId || this.ids;
      this.$modal
        .confirm('是否确认删除账套管理编号为"' + accountIds + '"的数据项？')
        .then(function () {
          return delAccountSet(accountIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "finance/AccountSet/export",
        {
          ...this.queryParams,
        },
        `AccountSet_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
